<?php declare(strict_types=1);

namespace App\Api\Handler;

use App\Service\LinkcenterService;
use Hyperf\HttpServer\Response;

class PaymentHandler
{

    public function wxNotify(Response $response, array $params): \Psr\Http\Message\ResponseInterface
    {
        if (!LinkcenterService::instance()->validateNotifySig($params))
            return $response->withStatus(400);
        getLogger('pay', 'WX-NOTIFY')->info(json_encode($params, JSON_UNESCAPED_UNICODE));
//        /** @var Order | null $order */
//        $order = Order::where(['order_no' => $params['package']['out_trade_no']])->first();
//        if (!$order) {
//            return $response->withStatus(404);
//        } elseif ($order->status == Order::STATUS_PAID) {
//            return $response->withStatus(200);
//        }
//        // 验证支付金额
//        $payAmount = $params['package']['amount']['payer_total'] / 100;
//        if ($payAmount != $order->amount) {
//            return $response->withStatus(400);
//        }
//        $payTime = new \DateTime($params['package']['success_time']);
//        // 支付时间
//        $payTimestamp = $payTime->setTimezone(new \DateTimeZone('Asia/Shanghai'))->getTimestamp();
        // todo 更新订单状态并进行相关的统计与通知操作
//        $updated = RespHelper::fail();
//        if (RespHelper::isSucceed($updated)) {
//            return $response->withStatus(200);
//        }
        return $response->withStatus(400);
    }

    public function aliNotify(Response $response, array $params): \Psr\Http\Message\ResponseInterface
    {
        if (!LinkcenterService::instance()->validateNotifySig($params))
            return $response->withStatus(400);
        getLogger('pay', 'AL-NOTIFY')->info(json_encode($params, JSON_UNESCAPED_UNICODE));
//        /** @var Order | null $order */
//        $order = Order::where(['order_id' => $params['package']['out_trade_no']])->first();
//        if (!$order) {
//            return $response->withStatus(404);
//        } elseif ($order->status == Order::STATUS_PAID) {
//            return $response->withStatus(200);
//        }
//        // 验证支付金额
//        $payAmount = $params['package']['receipt_amount'];
//        if ($payAmount != $order->amount) {
//            return $response->withStatus(400);
//        }
        // todo 更新订单状态并进行相关的统计与通知操作
//        $updated = RespHelper::fail();
//        if (RespHelper::isSucceed($updated)) {
//            return $response->withStatus(200);
//        }
        return $response->withStatus(400);
    }

}